package global_village.app;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class Utils {

	private static final String TAG = Utils.class.getSimpleName();
	
	private static ConnectivityManager cMgr = (ConnectivityManager) AppState.getCtx()
			.getSystemService(Context.CONNECTIVITY_SERVICE);
	
	/**
	 * Show Toast in the app context - in all the screens.
	 * @param message - enter the desire string message
	 * @author Noam Tzumie 
	 */
	public static void showToast(String message){
		Toast.makeText(AppState.getCtx(), message, Toast.LENGTH_LONG).show();
	}
	/**
	 * Checks if there an internet connection
	 * @return boolean
	 * @author Noam Tzumie
	 */
	public static boolean isInternetConnected() {
		Log.i(TAG, "< isInternetConnected >");

		boolean isConnectionPresent = false;
		NetworkInfo netInfo = cMgr.getActiveNetworkInfo();

		if ((netInfo != null) && (netInfo.getState() != null)) {
			isConnectionPresent = netInfo.getState().equals(State.CONNECTED);
		}
		
		Log.i(TAG, "< isInternetConnected > Status: " + isConnectionPresent);
		return isConnectionPresent;
	}
	
	/**
     * *DEBUG*
     * Getting the current location and show toast
     * with latitude and longitude.
     * @return Double[] with 2 cells - [latitude,longitude]
     * @author Noam Tzumie
     */
    public static Double[] getLocation(){
    	//TODO - change this method. 	
    	LocationManager lm = AppState.getLocationManager();
    	
        Criteria cri = new Criteria();
        String bestProvider = lm.getBestProvider(cri, true);
        
        if(bestProvider == null){
        	Utils.showToast("provider is null");
        	return null;
        }
        
        Location location = lm.getLastKnownLocation(bestProvider);
        
        if(location != null){
        	Double[] result = {location.getLatitude(),location.getLongitude()};
        	return result;
        }else{
        	Utils.showToast("Can't find location provider");
        }
        return null;
    }
    
    public static String getSettingsByKey(String key){
    	SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(AppState.getCtx());
    	return settings.getString(key, "");
    }
}
